//
//  UserTableViewCell.m
//  Systetagram
//
//  Created by 欧杨 on 2021/1/7.
//

#import <Masonry.h>

#import "UserTableViewCell.h"
#import "ProfileViewController.h"

@implementation UserTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
    }
    return self;
}

- (UIImageView *)avatar
{
    if (!_avatar) {
        _avatar = [[UIImageView alloc] init];
        _avatar.backgroundColor = UIColor.lightGrayColor;
        _avatar.userInteractionEnabled = YES;
        NSString *url = [NSString stringWithFormat:@"%@%@",@"http://47.107.247.52:8080/", self.model.avatar];
        NSURL *imageURL = [NSURL URLWithString:url];
        NSURLSessionDataTask *task = [NSURLSession.sharedSession dataTaskWithURL:imageURL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            if (error == nil) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.avatar setImage:[UIImage imageWithData:data]];
                    UITapGestureRecognizer *singleGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarClicked)];
                    [self.avatar addGestureRecognizer:singleGesture];
                });
            }
        }];
        [task resume];
    }
    return _avatar;
}

- (UILabel *)userNmae
{
    if (!_userNmae) {
        _userNmae = [[UILabel alloc] init];
        _userNmae.font = [_userNmae.font fontWithSize:20];
        _userNmae.textColor = UIColor.blackColor;
        _userNmae.text = self.model.username;
    }
    return _userNmae;
}

- (UIButton *)follow
{
    if (!_follow) {
        _follow = [UIButton buttonWithType:UIButtonTypeSystem];
        [_follow setTitle:@"关注" forState:UIControlStateNormal];
    }
    return _follow;
}

- (void)layoutSubviews
{
    //NSLog(@"%@",self.model);
    [self.contentView addSubview:self.avatar];
    [self.contentView addSubview:self.userNmae];
    [self.contentView addSubview:self.follow];
    
    [_avatar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake(60, 60));
        make.top.left.equalTo(self.contentView).offset(10);
    }];
    [_userNmae mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_avatar);
        make.left.equalTo(_avatar.mas_right).offset(10);
    }];
    [_follow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(50, 30));
        make.right.equalTo(self.contentView).offset(-20);
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = UIColor.lightGrayColor;
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_avatar.mas_bottom).mas_offset(10);
        make.height.mas_equalTo(1);
        make.left.equalTo(self.contentView).mas_offset(10);
        make.right.equalTo(self.contentView).mas_offset(-10);
    }];
}

- (void)avatarClicked
{
    ProfileViewController *VC = [[ProfileViewController alloc] initWithUserID:self.model.id];
    [[self viewController].navigationController pushViewController:VC animated:YES];
}

#pragma mark 获得当前view的控制器
- (UIViewController*)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController
                                          class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}

@end
